﻿using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;

public class GameMapFileManager
{
    protected static GameMapFileManager _Inst;
    public static GameMapFileManager Inst
    {
        get { if (_Inst == null) { _Inst = new GameMapFileManager(); } return _Inst; }
    }

    public GameMapData MapData;
    public MapBlockData BlockData;
    string DefaultBlockPath = "../Public/GameTable/Map/Blocks.xml";
     
    public GameMapData LoadMapData(string strPath)
    {
        MapData = new GameMapData(strPath);
        return MapData;
    }

    public MapBlockData LoadBlockData()
    {
        this.BlockData = new MapBlockData(DefaultBlockPath);
        return null;
    }

    public void SaveGameMap(GameMapData mapData)
    {
        XmlDocument xmldoc = new XmlDocument();
        XmlElement RootElem = xmldoc.CreateElement("Map");
        xmldoc.AppendChild(RootElem);
        XmlElement HeaderElem = mapData.Header.ToXml(xmldoc.CreateElement("MapHeader"));
        RootElem.AppendChild(HeaderElem);
        XmlElement LayersElem = xmldoc.CreateElement("MapBlockLayers");
        RootElem.AppendChild(LayersElem);
        for (int i = 0; i < mapData.LayerDatas.Length; i++)
        {
            XmlElement layerElem = xmldoc.CreateElement("BlockLayer");
            LayersElem.AppendChild(mapData.LayerDatas[i].ToXml(layerElem));
        }
        xmldoc.Save(mapData.Header.MapPath);
    }
}

